local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    -- Asset( "IMAGE", "images/inventoryimages/panda_fisherman_supply_pack.tex" ),  -- 背包贴图
    -- Asset( "ATLAS", "images/inventoryimages/panda_fisherman_supply_pack.xml" ),
}
local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_event_item_buyer"] or {}
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

local function itemCheck(item)
    local function band_check(theItemName_str)
        local Whitelist = {"eyebone","debugstone","name","goldnugget","npc_item_goldnugget_coin"}
        local ret = false
        for i ,iName in pairs(Whitelist) do
            if string.find(theItemName_str,iName) ~= nil then
                ret = true
                break
            end

        end
        return ret
    end 

    if item and item:IsValid() then
        if item:HasTag("nosteal") or item:HasTag("irreplaceable") then
            return false
        end

        if band_check(item.prefab) == true then
            return false
        end
    end

    return true
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

local function container_Widget_change(theContainer)
        -----------------------------------------------------------------------------------
    ----- 容器界面名 --- 要独特一点，避免冲突
    local container_widget_name = "npc_recycle_cart_widget"

    -----------------------------------------------------------------------------------
    ----- 检查和注册新的容器界面
    local all_container_widgets = require("containers")
    local params = all_container_widgets.params
    if params[container_widget_name] == nil then
        params[container_widget_name] = {
            widget =
            {
                slotpos = {},
                animbank = "ui_fish_box_5x4",
                animbuild = "ui_fish_box_5x4",
                pos = Vector3(0, 220, 0),
                side_align_tip = 160,
            },
            type = "chest",
            acceptsstacks = true,                
        }

        for y = 2.5, -0.5, -1 do
            for x = -1, 3 do
                table.insert(params[container_widget_name].widget.slotpos, Vector3(75 * x - 75 * 2 + 75, 75 * y - 75 * 2 + 75, 0))
            end
        end
        ------------------------------------------------------------------------------------------
        ---- item test
            params[container_widget_name].itemtestfn =  function(container_com, item, slot)
                return itemCheck(item)
            end
        ------------------------------------------------------------------------------------------
    end
    
    theContainer:WidgetSetup(container_widget_name)
    ------------------------------------------------------------------------
end

local function add_container_before_not_ismastersim_return(inst)
	-------------------------------------------------------------------------------------------------
    ------ 添加背包container组件    --- 必须在 SetPristine 之后，
    -- local container_WidgetSetup = "wobysmall"
    if TheWorld.ismastersim then
        inst:AddComponent("container")
        inst.components.container.openlimit = 1  ---- 限制1个人打开
        -- inst.components.container:WidgetSetup(container_WidgetSetup)
        container_Widget_change(inst.components.container)

    else
        inst.OnEntityReplicated = function(inst)
            container_Widget_change(inst.replica.container)
        end
    end
    -------------------------------------------------------------------------------------------------
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

local function recycle_task(inst)
    inst:WatchWorldState("cycles",function()
        inst:DoTaskInTime(math.random(10,60),function()
            if not inst:IsNearPlayer(20,true) then
                ------------------------------------------------------
                inst.components.container:ForEachItem(function(item)
                    if item and item:IsValid() then
                        local num = 1
                        if item.components.stackable then
                            num = item.components.stackable.stacksize
                        end
                        local base_num = math.random(4)/10
                        local ret_num = base_num*num
                        if TUNING.NPC_CONFIG.REWARDS_LESS == false then
                            ret_num = ret_num/2
                        end
                        
                        if item:HasTag("npc_item_woodlegs_cage_key") then
                            ret_num = 15
                        end

                        TheWorld.npc_everything_data:Add("recycle_point",ret_num)
                        item:Remove()
                    end
                end)
                -- print("info: recycle_task end")
                ------------------------------------------------------
                if TheWorld.npc_everything_data:Add("recycle_point",0) > 1 and TheWorld.npc_everything_data:Get("recycle_point_announce")~= true then
                    TheWorld.npc_everything_data:Set("recycle_point_announce",true)
                    if TUNING.NPC_CONFIG.RECYCLING_SETTLEMENT_ANNOUNCE == true then
                        TheWorld.npc_base_lib:Announce(GetStringsTable().recycle_exchange_announce)
                    end
                end
                ------------------------------------------------------
            end
        end)
    end)
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local function fn()
    TheSim:LoadPrefabs({ "quagmire_merm_cart1" })

    local inst = CreateEntity()
    inst.entity:AddTransform()
	inst.entity:AddNetwork()

    inst.entity:AddAnimState()
    inst.AnimState:SetBank("quagmire_mermcart")
    inst.AnimState:SetBuild("quagmire_mermcart")
    inst.AnimState:PlayAnimation("idle1")
    -- inst.AnimState:SetScale(0.5,0.5,0.5)

    MakeObstaclePhysics(inst,0.5)


    inst.entity:SetPristine()
    add_container_before_not_ismastersim_return(inst)
    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")


    inst:AddComponent("inspectable") --可检查组件

    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable().cart_name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_recycle_cart")] = GetStringsTable().cart_inspect_str  --人物检查的描述

    inst:DoTaskInTime(0,function()
        if inst.Ready ~= true then
            inst:Remove()
        end
    end)
    -----------------------------------------------------------------------------------
    recycle_task(inst)
    inst:ListenForEvent("itemget",function(inst,_table)
        if _table and _table.item then
            local tempItem = _table.item
            if tempItem.components.perishable then
                tempItem.components.perishable:StopPerishing()
            end
        end
    end)

    inst:ListenForEvent("itemlose",function(inst,_table)
        if _table and _table.prev_item then
            local tempItem = _table.prev_item
            if tempItem.components.perishable then
                tempItem.components.perishable:StartPerishing()
            end
        end
    end)
    -----------------------------------------------------------------------------------
    return inst
end

return Prefab("npc_item_recycle_cart", fn,assets)